<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>buttonrenderer.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_ui_buttonrenderer.js.html">buttonrenderer.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2008 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Default renderer for {@link goog.ui.Button}s.
<a name="line17"></a> *
<a name="line18"></a> * @author attila@google.com (Attila Bodis)
<a name="line19"></a> */
<a name="line20"></a>
<a name="line21"></a>goog.provide(&#39;goog.ui.ButtonRenderer&#39;);
<a name="line22"></a>
<a name="line23"></a>goog.require(&#39;goog.a11y.aria&#39;);
<a name="line24"></a>goog.require(&#39;goog.a11y.aria.Role&#39;);
<a name="line25"></a>goog.require(&#39;goog.a11y.aria.State&#39;);
<a name="line26"></a>goog.require(&#39;goog.asserts&#39;);
<a name="line27"></a>goog.require(&#39;goog.ui.ButtonSide&#39;);
<a name="line28"></a>goog.require(&#39;goog.ui.Component.State&#39;);
<a name="line29"></a>goog.require(&#39;goog.ui.ControlRenderer&#39;);
<a name="line30"></a>
<a name="line31"></a>
<a name="line32"></a>
<a name="line33"></a>/**
<a name="line34"></a> * Default renderer for {@link goog.ui.Button}s.  Extends the superclass with
<a name="line35"></a> * the following button-specific API methods:
<a name="line36"></a> * &lt;ul&gt;
<a name="line37"></a> *   &lt;li&gt;{@code getValue} - returns the button element&#39;s value
<a name="line38"></a> *   &lt;li&gt;{@code setValue} - updates the button element to reflect its new value
<a name="line39"></a> *   &lt;li&gt;{@code getTooltip} - returns the button element&#39;s tooltip text
<a name="line40"></a> *   &lt;li&gt;{@code setTooltip} - updates the button element&#39;s tooltip text
<a name="line41"></a> *   &lt;li&gt;{@code setCollapsed} - removes one or both of the button element&#39;s
<a name="line42"></a> *       borders
<a name="line43"></a> * &lt;/ul&gt;
<a name="line44"></a> * For alternate renderers, see {@link goog.ui.NativeButtonRenderer},
<a name="line45"></a> * {@link goog.ui.CustomButtonRenderer}, and {@link goog.ui.FlatButtonRenderer}.
<a name="line46"></a> * @constructor
<a name="line47"></a> * @extends {goog.ui.ControlRenderer}
<a name="line48"></a> */
<a name="line49"></a>goog.ui.ButtonRenderer = function() {
<a name="line50"></a>  goog.ui.ControlRenderer.call(this);
<a name="line51"></a>};
<a name="line52"></a>goog.inherits(goog.ui.ButtonRenderer, goog.ui.ControlRenderer);
<a name="line53"></a>goog.addSingletonGetter(goog.ui.ButtonRenderer);
<a name="line54"></a>
<a name="line55"></a>
<a name="line56"></a>/**
<a name="line57"></a> * Default CSS class to be applied to the root element of components rendered
<a name="line58"></a> * by this renderer.
<a name="line59"></a> * @type {string}
<a name="line60"></a> */
<a name="line61"></a>goog.ui.ButtonRenderer.CSS_CLASS = goog.getCssName(&#39;goog-button&#39;);
<a name="line62"></a>
<a name="line63"></a>
<a name="line64"></a>/**
<a name="line65"></a> * Returns the ARIA role to be applied to buttons.
<a name="line66"></a> * @return {goog.a11y.aria.Role|undefined} ARIA role.
<a name="line67"></a> * @override
<a name="line68"></a> */
<a name="line69"></a>goog.ui.ButtonRenderer.prototype.getAriaRole = function() {
<a name="line70"></a>  return goog.a11y.aria.Role.BUTTON;
<a name="line71"></a>};
<a name="line72"></a>
<a name="line73"></a>
<a name="line74"></a>/**
<a name="line75"></a> * Updates the button&#39;s ARIA (accessibility) state if the button is being
<a name="line76"></a> * treated as a checkbox.
<a name="line77"></a> * @param {Element} element Element whose ARIA state is to be updated.
<a name="line78"></a> * @param {goog.ui.Component.State} state Component state being enabled or
<a name="line79"></a> *     disabled.
<a name="line80"></a> * @param {boolean} enable Whether the state is being enabled or disabled.
<a name="line81"></a> * @protected
<a name="line82"></a> * @override
<a name="line83"></a> */
<a name="line84"></a>goog.ui.ButtonRenderer.prototype.updateAriaState = function(element, state,
<a name="line85"></a>    enable) {
<a name="line86"></a>  // If button has CHECKED state, assign ARIA atrribute aria-pressed
<a name="line87"></a>  if (state == goog.ui.Component.State.CHECKED) {
<a name="line88"></a>    goog.asserts.assert(element,
<a name="line89"></a>        &#39;The button DOM element cannot be null.&#39;);
<a name="line90"></a>    goog.a11y.aria.setState(element, goog.a11y.aria.State.PRESSED, enable);
<a name="line91"></a>  } else {
<a name="line92"></a>    goog.ui.ButtonRenderer.superClass_.updateAriaState.call(this, element,
<a name="line93"></a>        state, enable);
<a name="line94"></a>  }
<a name="line95"></a>};
<a name="line96"></a>
<a name="line97"></a>
<a name="line98"></a>/** @override */
<a name="line99"></a>goog.ui.ButtonRenderer.prototype.createDom = function(button) {
<a name="line100"></a>  var element = goog.ui.ButtonRenderer.superClass_.createDom.call(this, button);
<a name="line101"></a>
<a name="line102"></a>  var tooltip = button.getTooltip();
<a name="line103"></a>  if (tooltip) {
<a name="line104"></a>    this.setTooltip(element, tooltip);
<a name="line105"></a>  }
<a name="line106"></a>
<a name="line107"></a>  var value = button.getValue();
<a name="line108"></a>  if (value) {
<a name="line109"></a>    this.setValue(element, value);
<a name="line110"></a>  }
<a name="line111"></a>
<a name="line112"></a>  // If this is a toggle button, set ARIA state
<a name="line113"></a>  if (button.isSupportedState(goog.ui.Component.State.CHECKED)) {
<a name="line114"></a>    this.updateAriaState(element, goog.ui.Component.State.CHECKED,
<a name="line115"></a>                         button.isChecked());
<a name="line116"></a>  }
<a name="line117"></a>
<a name="line118"></a>  return element;
<a name="line119"></a>};
<a name="line120"></a>
<a name="line121"></a>
<a name="line122"></a>/** @override */
<a name="line123"></a>goog.ui.ButtonRenderer.prototype.decorate = function(button, element) {
<a name="line124"></a>  // The superclass implementation takes care of common attributes; we only
<a name="line125"></a>  // need to set the value and the tooltip.
<a name="line126"></a>  element = goog.ui.ButtonRenderer.superClass_.decorate.call(this, button,
<a name="line127"></a>      element);
<a name="line128"></a>
<a name="line129"></a>  button.setValueInternal(this.getValue(element));
<a name="line130"></a>  button.setTooltipInternal(this.getTooltip(element));
<a name="line131"></a>
<a name="line132"></a>  // If this is a toggle button, set ARIA state
<a name="line133"></a>  if (button.isSupportedState(goog.ui.Component.State.CHECKED)) {
<a name="line134"></a>    this.updateAriaState(element, goog.ui.Component.State.CHECKED,
<a name="line135"></a>                         button.isChecked());
<a name="line136"></a>  }
<a name="line137"></a>
<a name="line138"></a>  return element;
<a name="line139"></a>};
<a name="line140"></a>
<a name="line141"></a>
<a name="line142"></a>/**
<a name="line143"></a> * Takes a button&#39;s root element, and returns the value associated with it.
<a name="line144"></a> * No-op in the base class.
<a name="line145"></a> * @param {Element} element The button&#39;s root element.
<a name="line146"></a> * @return {string|undefined} The button&#39;s value (undefined if none).
<a name="line147"></a> */
<a name="line148"></a>goog.ui.ButtonRenderer.prototype.getValue = goog.nullFunction;
<a name="line149"></a>
<a name="line150"></a>
<a name="line151"></a>/**
<a name="line152"></a> * Takes a button&#39;s root element and a value, and updates the element to reflect
<a name="line153"></a> * the new value.  No-op in the base class.
<a name="line154"></a> * @param {Element} element The button&#39;s root element.
<a name="line155"></a> * @param {string} value New value.
<a name="line156"></a> */
<a name="line157"></a>goog.ui.ButtonRenderer.prototype.setValue = goog.nullFunction;
<a name="line158"></a>
<a name="line159"></a>
<a name="line160"></a>/**
<a name="line161"></a> * Takes a button&#39;s root element, and returns its tooltip text.
<a name="line162"></a> * @param {Element} element The button&#39;s root element.
<a name="line163"></a> * @return {string|undefined} The tooltip text.
<a name="line164"></a> */
<a name="line165"></a>goog.ui.ButtonRenderer.prototype.getTooltip = function(element) {
<a name="line166"></a>  return element.title;
<a name="line167"></a>};
<a name="line168"></a>
<a name="line169"></a>
<a name="line170"></a>/**
<a name="line171"></a> * Takes a button&#39;s root element and a tooltip string, and updates the element
<a name="line172"></a> * with the new tooltip.
<a name="line173"></a> * @param {Element} element The button&#39;s root element.
<a name="line174"></a> * @param {string} tooltip New tooltip text.
<a name="line175"></a> * @protected
<a name="line176"></a> */
<a name="line177"></a>goog.ui.ButtonRenderer.prototype.setTooltip = function(element, tooltip) {
<a name="line178"></a>  if (element) {
<a name="line179"></a>    element.title = tooltip || &#39;&#39;;
<a name="line180"></a>  }
<a name="line181"></a>};
<a name="line182"></a>
<a name="line183"></a>
<a name="line184"></a>/**
<a name="line185"></a> * Collapses the border on one or both sides of the button, allowing it to be
<a name="line186"></a> * combined with the adjacent button(s), forming a single UI componenet with
<a name="line187"></a> * multiple targets.
<a name="line188"></a> * @param {goog.ui.Button} button Button to update.
<a name="line189"></a> * @param {number} sides Bitmap of one or more {@link goog.ui.ButtonSide}s for
<a name="line190"></a> *     which borders should be collapsed.
<a name="line191"></a> * @protected
<a name="line192"></a> */
<a name="line193"></a>goog.ui.ButtonRenderer.prototype.setCollapsed = function(button, sides) {
<a name="line194"></a>  var isRtl = button.isRightToLeft();
<a name="line195"></a>  var collapseLeftClassName =
<a name="line196"></a>      goog.getCssName(this.getStructuralCssClass(), &#39;collapse-left&#39;);
<a name="line197"></a>  var collapseRightClassName =
<a name="line198"></a>      goog.getCssName(this.getStructuralCssClass(), &#39;collapse-right&#39;);
<a name="line199"></a>
<a name="line200"></a>  button.enableClassName(isRtl ? collapseRightClassName : collapseLeftClassName,
<a name="line201"></a>      !!(sides &amp; goog.ui.ButtonSide.START));
<a name="line202"></a>  button.enableClassName(isRtl ? collapseLeftClassName : collapseRightClassName,
<a name="line203"></a>      !!(sides &amp; goog.ui.ButtonSide.END));
<a name="line204"></a>};
<a name="line205"></a>
<a name="line206"></a>
<a name="line207"></a>/** @override */
<a name="line208"></a>goog.ui.ButtonRenderer.prototype.getCssClass = function() {
<a name="line209"></a>  return goog.ui.ButtonRenderer.CSS_CLASS;
<a name="line210"></a>};
</pre>


</body>
</html>
